layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //Banner列表
    var tableIns = table.render({
        elem: '#category_list',
        url : '/listCategory',
        cellMinWidth : 95,
        page : true,
        height : "full",
        id : "category_list_table",
        limit : 20,
        limits : [20],
        cols : [[
            {field: 'name', title: '分类名称',  align:"center"},
            {field: 'displayOrder', title: '排序',align:"center",width:100},
            {field: 'statusStr', title: '状态', align:'center',width:150},
            {title: '操作', width:170, templet:'#category_list_bar',align:"center"}
        ]],
        response:{
            countName: 'total' //数据总数的字段名称，默认：count
            ,dataName: 'data' //数据列表的字段名称，默认：data
        }
    });

    //添加Banner
    function addCategory(data){
        var index = layer.open({
            title : "添加商品分类",
            type : 2,
            area : ["500px","280px"],
            content : "/page/category/categoryAdd.html",
            success : function(layero, index){
                var body = $($(".layui-layer-iframe",parent.document).find("iframe")[0].contentWindow.document.body);
                if(data){
                    body.find("input[name=id]").val(data.id);
                    body.find("input[name=categoryName]").val(data.name);
                    body.find("input[name=displayOrder]").val(data.displayOrder);
                    body.find("select[name=status]").attr("data-value",data.status);
                    form.render();
                }
            }
        })

        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }

    $(".addCategory_btn").click(function(){
        addCategory();
    });

    //列表操作
    table.on('tool(category_list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addCategory(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此商品分类？',{icon:3, title:'提示信息'},function(index){
                $.get("/deleteCategoryById",{
                    id : data.id
                },function(data){
                    tableIns.reload();
                    layer.close(index);
                })
            });
        }
    });
})